// https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/


func removeDuplicates(nums []int) int {
    i, c := 0, 1
	for i < len(nums) {
		i++
		for i < len(nums) && nums[i] == nums[i - 1] {
			i++
			c++
		}
		if c > 2 {
			if i < len(nums) {
				for j := i; j < len(nums); j++ {
					nums[j - (c - 2)] = nums[j]
				}
				i -= c - 2
				nums = nums[:n - (c - 2)]
			} else {
				nums = nums[:n - (c - 2)]
				break
			}
		}
		c = 1
	}
	return len(nums)
}